<template>
	<view class="main">
		<view class="card-bot">
			<view class="nav-item">
				<view class="left">

					<text>获取昵称</text>
				</view>

				<u-switch activeColor="#5ac725" size="20" inactiveColor="rgb(230, 230, 230)" v-model="author.userInfo"
					@change="change(author.userInfo,'userInfo')"></u-switch>
			</view>
			<view class="tips">
				* 授权打开用户微信登录，获取用户头像、昵称
			</view>
			<view class="nav-item">
				<view class="left">

					<text>获取定位信息</text>
				</view>

				<u-switch activeColor="#5ac725" size="20" inactiveColor="rgb(230, 230, 230)"
					v-model="author.userLocation" @change="change(author.userLocation,'userLocation')"></u-switch>
			</view>
			<view class="tips">
				* 授权打开用户微信定位，以便于获取当前位置信息
			</view>

			<view class="nav-item">
				<view class="left">

					<text>订单通知</text>
				</view>

				<u-switch activeColor="#5ac725" size="20" inactiveColor="rgb(230, 230, 230)" v-model="author.orderMsg"
					@change="change(author.orderMsg,'orderMsg')"></u-switch>
			</view>
			<view class="tips">
				* 授权打开用户订单通知
			</view>
			<view class="nav-item">
				<view class="left">

					<text>店铺审核通知</text>
				</view>

				<u-switch activeColor="#5ac725" size="20" inactiveColor="rgb(230, 230, 230)" v-model="author.shopMsg"
					@change="change(author.shopMsg,'shopMsg')"></u-switch>
			</view>
			<view class="tips">
				* 授权打开用户审核通知，微信消息推送
			</view>
			<view class="nav-item">
				<view class="left">

					<text>店铺通知</text>
				</view>

				<u-switch activeColor="#5ac725" size="20" inactiveColor="rgb(230, 230, 230)" v-model="author.storeMsg"
					@change="change(author.storeMsg,'storeMsg')"></u-switch>
			</view>
			<view class="tips">
				* 授权打开用户审核通知，微信消息推送
			</view>

			<view class="nav-item hc">
				<view class="left">
					<text>小程序缓存</text>
				</view>

				<view class="right">
					<text class="name">{{currentSize}}</text> <u-icon name="arrow-right" color="#333"
						size="16"></u-icon>

				</view>
			</view>
			<view class="tips">

			</view>
			<view class="nav-item hc">
				<view class="left">

					<text>检查版本</text>
				</view>

				<view class="right">
					<text class="name">{{version || '1.0.1'}}</text>
				</view>
			</view>
			<view class="tips">


			</view>

		</view>
	</view>
</template>

<script>
	// import {
	// 	getUserInfo
	// } from '../api'
	export default {
		data() {
			return {
				value: false,
				currentSize: "",
				version: "",
				author: {
					address: true,
					userInfo: true,
					userLocation: true,
					camera: true,
					mainSwitch: true,
					orderMsg: false, //订单审核通知
					shopMsg: false, //店铺审核通知
					storeMsg: false //店铺通知
				}
			}
		},
		methods: {
			changeTemp(status, id) {
				let that = this
				if (!status) {
					// getUserInfo().then(res => {
					// 	const tmplIds = [
					// 		id
					// 	]
					// 	uni.requestSubscribeMessage({
					// 		tmplIds,
					// 		success(res) {
					// 			console.log('订阅结果:', res)
					// 			if (res[tmplIds[0]] === 'accept') {
					// 				console.log('✅ 用户已打开通知')
					// 				that.getList()
					// 			} else {
					// 				console.log('❌ 用户拒绝或未授权通知')
					// 				that.getList()
					// 			}
					// 		},
					// 		fail(err) {
					// 			console.error('请求失败:', err)
					// 		}
					// 	})
					// })

				} else {
					uni.openSetting({
						withSubscriptions: true,
						success(res) {
							console.log('通知权限情况:', res.subscriptionsSetting)
							that.getList()
						}

					})
				}

			},
			change(item, name) {
				switch (name) {
					case 'userInfo':
						console.log('处理用户信息');
						break;
					case 'userLocation':
						console.log('处理用户定位');
						break;
					case 'orderMsg':
						this.changeTemp(item, "RpevymwxrZCbMYgiaIyTHc0YOWLbSCskL0NEidcLg48")
						console.log('处理订单消息');
						break;
					case 'shopMsg':
						this.changeTemp(item, "uUxjdW8Nad9MeTsGfCJVKe__GckU2x64m79mXARvdLE")
						console.log('处理门店消息');
						break;
					case 'storeMsg':
						this.changeTemp(item, "R2mE4FSJM5Xoe0nD2-Ymp630VjYo09qsTbQ0beDRBNU")
						console.log('处理门店消息');
						break;
					default:
						console.log('未知类型:', name);
				}
			},
			getList() {
				const tempIds = [
					"RpevymwxrZCbMYgiaIyTHc0YOWLbSCskL0NEidcLg48", //审核结果
					"uUxjdW8Nad9MeTsGfCJVKe__GckU2x64m79mXARvdLE", //店铺审核结果
					"R2mE4FSJM5Xoe0nD2-Ymp630VjYo09qsTbQ0beDRBNU" //店铺通知
				];
				let that = this
				uni.getSetting({
					withSubscriptions: true,
					success(res) {
						const settings = res.subscriptionsSetting || {};
						const itemSettings = settings.itemSettings || {};

						const result = tempIds.map(id => {
							const status = itemSettings[id];
							return {
								templateId: id,
								status: status || "none" // none 表示未处理
							};
						});

						console.log("订阅消息权限检测结果:", result);
						console.log(this)
						// 你也可以进一步分类处理
						result.forEach(item => {
							switch (item.status) {
								case "accept":
									item.templateId === tempIds[0] ? that.author.orderMsg = true : null
									item.templateId === tempIds[1] ? that.author.shopMsg = true : null
									item.templateId === tempIds[2] ? that.author.storeMsg = true : null

									console.log(`✅ 模板 ${item.templateId} 已授权`);
									break;

								default:
									item.templateId === tempIds[0] ? that.author.orderMsg = false : null
									item.templateId === tempIds[1] ? that.author.shopMsg = false : null
									item.templateId === tempIds[2] ? that.author.storeMsg = false : null
									console.log(`⚠️ 模板 ${item.templateId} 用户还未处理过`);
							}
						});

						console.log(that.author)
						console.log(res)
					}
				});

				uni.getStorageInfo({
					success: function(res) {
						console.log('当前缓存大小:', res.currentSize + ' KB');
						console.log('最大可用大小:', res.limitSize + ' KB');
						console.log('缓存 keys:', res.keys);
						that.currentSize = res.currentSize + ' KB'
					}
				});
				const accountInfo = uni.getAccountInfoSync();
				console.log('小程序 版本号:', accountInfo.miniProgram.version); // 只有正式版才有值
				that.version = accountInfo.miniProgram.version
			}
		},
		onShow() {
			// this.getList()
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		padding: 0rpx 0;
		box-sizing: border-box;
		background: #fff;
		min-height: 100vh;
	}

	.card-bot {
		width: 100%;
		min-height: 300rpx;
		background: #fff;
		border-radius: 10rpx;
		// margin-top: -40rpx;
		padding: 30rpx;
		box-sizing: border-box;

		.nav-item {
			width: 100%;
			// height: 80rpx;
			display: flex;
			margin-bottom: 10rpx;
			align-items: center;
			justify-content: space-between;
			color: #333;
			font-size: 28rpx;
			// border-bottom: 1rpx solid #f6f6f6;

			.left {
				width: 80%;
				display: flex;
				align-items: center;

				.bto-icon {
					margin-right: 6rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;

				.name {
					margin-top: 5rpx;
				}
			}

			&:nth-last-child(1) {
				border-bottom: none;
			}
		}

		.tips {
			width: 100%;
			font-size: 24rpx;
			color: #9f9f9f;
			margin-bottom: 30rpx;
		}


	}

	.btn-logout {
		margin-top: 25rpx;
		width: 100%;
		height: 80rpx;
		background: cornflowerblue;
		border-radius: 10rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 28rpx;
		color: #fff;
	}
</style>